home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 11 / Cream of the Crop 11-2.iso / extra / helpful.zip / keystrok < prev    next >
Text File  |  1995-04-08  |  10KB  |  279 lines

  1. =================== Keystroke-HOWTU, version 2.0 ====================
  2.  
  3.             Linux Keystroke HOWTO
  4.                      by
  5.          Zenon Fortuna (zenon@netcom.com)
  6.  
  7.  
  8. Version: 2.0                        April 4th '95
  9.  
  10. INDEX
  11.     0.    What is "Keystroke-HOWTO" ?
  12.     1.    History of changes
  13.     2.    Short description
  14.     3.    Tools for keyboard driver modification
  15.     4.    Modifying keytable file
  16.     4.0    Example of keytable file modification
  17.     4.1    Temporary modification of the keyboard setup
  18.     4.2    Permanent modification
  19.     5.    Example of the key_macro script
  20.     6.    Comments
  21.     7.    Further ideas ?
  22.  
  23. ----------------------------------------------------------------------------
  24. 0.    What is "Keystroke-HOWTO" ?
  25.  
  26.     This document is for users, who want to assign special action to some
  27.     of keys of the keyboard. The suggested method is to use the loadkeys(1)
  28.     or to modify the defkeymap.c file and relink the kernel.
  29.  
  30.     This text does NOT discuss remapping of keyboard keys, e.g. Backspace
  31.     or Delete keys. For informations about remapping of keys read the
  32.     Backspace Mini-HOWTO written by Stephen Lee.
  33.  
  34.     The method described below was tested on Linux 1.2.1 release, packaged
  35.     in the Slackware 2.2.0 distribution.
  36.  
  37.  
  38. 1.    History of changes
  39.  
  40.     April 4th '95    Version: 2.0
  41.        Adapted for the Linux 1.2.1:
  42.        - simple changes in suggested modification of the "my_keytable.map"
  43.        - modified "key_macro" example with /dev/vcs* screen dumping
  44.  
  45.     May   7th '94    Version: 1.0
  46.        The initial version of the "Keystroke-HOWTO", which worked for the
  47.        Linux 1.0
  48.  
  49.  
  50. 2.    Short description
  51.  
  52.     The Linux virtual terminal and keyboard drivers assume default keyboard
  53.     mapping as defined in the drivers/char/defkeymap.c file of the kernel
  54.     source. The 12 PC keyboard function keys may get strings assigned to
  55.     their action. After pressing any of those function keys, perhaps
  56.     modified with the Alt or Ctrl keys, the current virtual terminal
  57.     adds the specific string to its input and output buffers, in effect
  58.     emulating entry of this string as typed in from the keyboard.
  59.  
  60.     Setting an appropriate string for chosen function key, we can simplify
  61.     execution of selected command, for example calling a Shell-script
  62.     "/usr/local/bin/key_macro", which we can create and modify as desired.
  63.  
  64.  
  65. 3.    Tools for keyboard driver modification
  66.  
  67.     We may use loadkeys(1), dumpkeys(1) and showkey(1):
  68.       The loadkeys(1) utility helps to load new strings into the kernel
  69.     keyboard buffers or prepares the new C-code to modify the kernel.
  70.       The dumpkeys(1) should be used to get the current keyboard mapping
  71.     table for inspection or modification.
  72.       The showkey(1) may assist us to obtain the keycode of the selected
  73.     function key.
  74.  
  75.     If your Linux system does not have these utilities, you may get them
  76.     via anonymous ftp as kbd-0.89.tar.gz or kbd-0.90.tar.gz package from
  77.         sunsite.unc.edu:/pub/Linux/system/Keyboards, or
  78.         tsx-11.mit.edu:/pub/linux/sources/system
  79.     You should use the GNU tar to extract the needed files.
  80.  
  81.  
  82. 4.    Modifying keytable file
  83.  
  84.     Linux kernel includes compiled defkeymap.c code, which is generated
  85.     with the loadkeys(1) utility from a defkeymap.map file. Both files
  86.     are included in the src/linux/drivers/char directory.
  87.  
  88.     We need to modify the defkeymap.map file, so let's make a local
  89.     copy of it either by
  90.         # cp defkeymap.map my_keytable.map
  91.     or
  92.         # dumpkeys > my_keytable.map
  93.  
  94.     There is also a large collection of different keytable files in the
  95.     /usr/lib/kbd/keytables directory, from which "defkeymap.map" may be
  96.     used as src/linux/drivers/char/defkeymap.map file on your system.
  97.  
  98.     The method which uses the dumpkeys(1) utility is recommended, because
  99.     it may happen, that your kernel was already modified or generated
  100.     for you with different defkeymap.map file than the one you can find.
  101.  
  102.     Lets read the contents of the my_keytable.map file: there are more
  103.     than 300 lines of code, and we can find 3 groups of declarations:
  104.     The first group consists of lines with the word "keycode", maybe
  105.     prepended with additional words like "alt", "control", etc.
  106.     The second group consists of lines with the word "string".
  107.     The third group consists of lines with the word "compose".
  108.  
  109.     More about the keytables(5) syntax can be read with
  110.         % man keytables
  111.  
  112.  
  113. 4.0    Example of keytable file modification
  114.  
  115.     As an example of assigning a macro-string to a function key stroke,
  116.     let's make the "Ctrl-F1" to call our "/usr/local/bin/key_macro"
  117.     Shell-script.
  118.  
  119.     First of all we should find out what is the keycode for the F1 function
  120.     key.  We may use the showkey(1) utility to find the keycode with
  121.     pressing F1.
  122.  
  123.     Instead we can search for the "F1" string in the "my_keytable.map" file
  124.     to find the following line:
  125.  
  126.     keycode  59 = F1
  127.  
  128.     This suggests, that the keycode for the F1 function key is 59.
  129.     This line defines also, that after pressing the F1 key the keyboard
  130.     driver would send out the string denoted by the string-code "F1".
  131.     To see the contents of this string, one can search for the "string F1"
  132.     pattern, to find
  133.  
  134.     string F1 = "\033[[A"
  135.  
  136.     This means, that after pressing the F1 key, the keyboard driver sends
  137.     the "Esc [ [ A" (without blank spaces).
  138.  
  139.     We shouldn't change this string, because some applications depend on
  140.     this string as default action of the F1 function key.
  141.  
  142.     However, we may define the new action for Ctrl-F1, provided it is not
  143.     reserved by your kernel for other special actions.
  144.     To see the mappings of the F1 key modified with the Ctrl-, Shift- or
  145.     other modes, we may inspect the "my_keytable.map" file with
  146.  
  147.         # grep 59 my_keytable.map
  148.  
  149.     In case when there is no line with "control keycode 59" we may use
  150.     the Ctrl-F1 without problems. (when a line with the "shift control
  151.     keycode 59" exists it is still OK)
  152.  
  153.     Let us add a following line to the "my_keytable.map" file:
  154.  
  155.         control keycode 59 = key_symbol
  156.  
  157.     where the "key_symbol" would define the action of the Ctrl-F1 key.
  158.     The Linux 1.2.* allows a dynamic allocation of strings, but a name
  159.     of the "key_symbol" may be picked up only from a fixed set of names.
  160.     Between other names the key-symbols F1-F246 are allowed.
  161.     For "my_keytable.map" on my system the F21 was unused, but you should
  162.     inspect your "my_keytable.map" and choose a proper key-symbol.
  163.     So, we may end up with a line
  164.  
  165.         control keycode 59 = F21
  166.  
  167.     Now we have to define the contents of the F21, adding a line
  168.  
  169.         string F21 = "/usr/local/bin/key_macro\n"
  170.  
  171.     In the summary, we made two changes to the original "my_keytable.map"
  172.     file: we declared the new string F21, and we have declared
  173.     that the Ctrl-F1 key will be calling the F21 contents.
  174.  
  175.  
  176. 4.1    Temporary modification of the keyboard setup
  177.  
  178.     Having properly modified "my_keytable.map" we can copy the changes
  179.     to the kernel keyboard driver, using the loadkeys(1) utility:
  180.  
  181.     % loadkeys my_keytable.map
  182.  
  183.     The permission to modify the kernel keyboard driver is granted to
  184.     everybody who has the read access to the "/dev/console" device.
  185.  
  186.     To verify that the intended changes were installed, we can use the
  187.     dumpkeys(1) utility to check the F21 value, for example
  188.  
  189.     % dumpkeys | grep F21
  190.  
  191.     We may see:
  192.  
  193.     keycode 59 = F1        F11    Console_13    F21
  194.  
  195.     string F21 = "/usr/local/bin/key_macro\012"
  196.  
  197.     which is OK, because "\012", or LF, is equivalent to "\n".
  198.  
  199.     Now, pressing "Ctrl-F1" should call the "/usr/local/bin/key_macro"
  200.     Shell-script, as intended. 
  201.  
  202.  
  203. 4.2    Permanent modification
  204.  
  205.     The changes to the kernel keyboard driver imposed by the loadkeys(1)
  206.     last until the next reboot (or the next call to loadkeys).
  207.  
  208.     We can modify the /etc/rc.d/rc.local to call the loadkeys with our
  209.     my_keytable.map file as an argument. Instead, we can modify the
  210.     src/linux/drivers/char/defkeymap.c and re-link the kernel with
  211.     new defaults.
  212.  
  213.     We should not modify the defkeymap.c manually, but rather generate
  214.     it with the loadkeys(1) utility:
  215.  
  216.     # mv defkeymap.c defkeymap.c.ORIG
  217.     # loadkeys --mktable my_keytable.map > defkeymap.c
  218.  
  219.     Then we should generate the new kernel, essentially changing directory
  220.     to the root of the linux kernel source, and using the make(1).
  221.  
  222.     Finally, we should use the lilo(1) to install and boot our new kernel.
  223.  
  224.  
  225. 5.    Example of the key_macro script
  226.  
  227.     A particularly useful script for simple-key-stroke operation may be
  228.     a Shell-script preparing, or printing, a screen dump.
  229.  
  230.     This example has changed since the version 1.0 of Linux, because
  231.     of the changes in the Linux kernel, which does not provide the
  232.     ioctl(0,TIOCLINUX) system call anymore.
  233.  
  234.     To read the virtual console screen dumps one should prepare first
  235.     some device files. As "root" user we may create the following files:
  236.  
  237.         # mknod /dev/vcs1  c 7 1
  238.         # mknod /dev/vcs2  c 7 2
  239.         ...
  240.         # mknod /dev/vcs63 c 7 63
  241.  
  242.     Of course, it is sufficient to have only the /dev/vcs* files specific
  243.     for the virtual consoles you are using.
  244.  
  245.     The code below should be regarded as an example of possible
  246.     /usr/local/bin/key_macro file:
  247.  
  248. #!/bin/sh
  249. #
  250. # This is an example of useful key_macro script
  251. #
  252.  
  253. VT_NUMBER=`tty|cut -c9-`
  254. FILE=/tmp/vt$VT_NUMBER.dump
  255. cp /dev/vcs$VT_NUMBER $FILE
  256. echo SCREEN DUMP saved in $FILE
  257. #
  258. # Uncomment the line below if you want to print the resulted dump-file
  259. # lpr $FILE
  260.  
  261.  
  262. 6.    Comments
  263.  
  264.     There is no practical limit on the sum the lengths of all strings
  265.     which we would like to load to the keyboard driver. The previous fixed
  266.     buffer of the length of FUNC_BUFSIZE (set to 512 bytes), has been
  267.     replaced in the Linux 1.2.* by a strategy of dynamic buffer allocation
  268.     ... in chunks of 512 bytes each.
  269.  
  270.     The most recent copy of the Keystroke-HOWTO can be found in
  271.  
  272.         ftp.netcom.com: /pub/ze/zenon/linux/howto
  273.  
  274.  
  275. 7.    Further ideas ?
  276.  
  277.     In case you find anything worth adding to this document, please send
  278.     your comments to zenon@netcom.com -- thanks (zf)
  279.